package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.authentication.UpdateEnabledUserActiveTokenDto
import com.opennews.openplatform.familyexpenseapi.service.UserActiveTokenService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/user-active-token")
class UserActiveTokenController(
    private val userActiveTokenService: UserActiveTokenService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/query-device-by-username")
    fun queryDeviceByUsername(): ResponseEntity<ApiResponse<List<Map<String, Any>>>> {
        val username = requestIdentityExtractor.getClaims().username
        val result = userActiveTokenService.queryDeviceByUsername(username)

        return getSuccessResponse(result)
    }

    @PostMapping("/update-enabled")
    fun updateEnabled(@Valid @RequestBody data: UpdateEnabledUserActiveTokenDto): ResponseEntity<ApiResponse<Any?>> {
        val id = data.id.asString()
        val enabled = data.enabled
        val username = requestIdentityExtractor.getClaims().username
        userActiveTokenService.updateEnabled(id, username, enabled)

        return getSuccessResponse()
    }

    @PostMapping("/delete")
    fun delete(@Valid @RequestBody data: IdentityDto): ResponseEntity<ApiResponse<Any?>> {
        val id = data.id.asString()
        val username = requestIdentityExtractor.getClaims().username
        userActiveTokenService.delete(id, username)

        return getSuccessResponse()
    }
}